package org.jxc.service;

import java.util.Date;

import org.hibernate.SessionFactory;
import org.jxc.constants.JXCString;
import org.jxc.dao.SalesOrderDAO;
import org.jxc.domain.SalesOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.team4.dao.DAOFactory;
import org.team4.domain.Users;
import org.team4.exception.CRUDException;
import org.team4.service.IService;
import org.team4.util.LoginBean;
import org.team4.vo.Page;

public class SalesOrderService implements IService {

	private SalesOrderDAO salesOrderDAO;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		salesOrderDAO = (SalesOrderDAO) DAOFactory.getDAO(sessionFactory,
				SalesOrderDAO.class);
	}

	public Page<SalesOrder> listPagination(Page<SalesOrder> page)
			throws CRUDException {
		Users user = LoginBean.getLoginBean().getUser();
		if (user.getDepartment().getManager().getId().equals(user.getId()) ) {
			return salesOrderDAO.pagination(page,
					"from SalesOrder where deleteFlag='0'", "createTime desc");
		}else{
			return salesOrderDAO.pagination(page,
					"from SalesOrder where deleteFlag='0'" +
					" and creator.id= '"+ user.getId() +"'", "createTime desc");
		}
		
	}

	public Page<SalesOrder> listPaginationForChoose(Page<SalesOrder> page)
			throws CRUDException {
		return salesOrderDAO.pagination(page,
				"from SalesOrder where deleteFlag='0' and outState=1",
				"createTime desc");
	}

	/**
	 * 根据id查询商品
	 * 
	 * @param id
	 * @return
	 * @throws CRUDException 
	 * @throws Exception
	 */
	public SalesOrder findById(String id) throws CRUDException {
		SalesOrder so = salesOrderDAO.get(id);
		return so;
	}

	/**
	 * 新建销售订单
	 * 
	 * @param SaleOrder
	 * @throws CRUDException
	 */
	public void saveSaleOrder(SalesOrder so) throws CRUDException {
		so.setDeleteFlag("0");
		so.setAuditState(JXCString.NOTAPPLY);
		so.setOutState(JXCString.NOTOUTSTORE);
		so.setAuditor(LoginBean.getLoginBean().getUser().getDepartment()
				.getManager());
		so.setCreateTime(new Date());
		so.setCreator(LoginBean.getLoginBean().getUser());
		salesOrderDAO.save(so);
	}

	/**
	 * 修改商品信息
	 * 
	 * @param SaleOrder
	 * @throws CRUDException
	 */
	public void updateSalesOrder(SalesOrder so) throws CRUDException {
		SalesOrder old = salesOrderDAO.get(so.getId());
		old.setBillCode(so.getBillCode());
		old.setOrderDate(so.getOrderDate());
		old.setSaler(so.getSaler());
		old.setCustomer(so.getCustomer());
		old.setTotalPrice(so.getTotalPrice());
		old.setRemark(so.getRemark());
		old.setEditor(LoginBean.getLoginBean().getUser());
		old.setEditTime(new Date());
		salesOrderDAO.update(old);
	}

	/**
	 * 批量删除销售订单
	 * 
	 * @param idList
	 * @return
	 * @throws CRUDException
	 */
	public void deleteSalesOrder(String id) throws CRUDException {
		SalesOrder so = salesOrderDAO.get(id);
		so.setDeleteFlag("1");
		salesOrderDAO.update(so);
	}

	public Page<Object> getSalesCount(Page<Object> page, int year) {
		return salesOrderDAO.findSalesCount(page, year);
	}
}
